<%= form_for(@product) do |f| %>
  <% if @product.errors.any? %>
    <div id="error_explanation", class="red">
      <ul>
      <% @product.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name, required: true %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.text_field :price, required: true %>
  </div>
  <div class="field">
    <%= f.label :reward %>(%)<br>
    <%= f.text_field :reward, required: true %>
  </div>
  <div class="field">
    <%= f.label "销售时段" %><br>
    <%= f.text_field :sale_start_at, value: @product.sale_start_at.try(:strftime, '%F') , placeholder: '开始' %>
    <%= f.text_field :sale_end_at, value: @product.sale_end_at.try(:strftime, '%F'), placeholder: '结束' %>
    <br/>
    <span class="help-block">销售时段不填则为一直有效</span>
  </div>
  <div class="field">
    <%= f.label :validity_type %><br>
    <%= f.select :validity_type, [['一直有效', 'forever'], ['天', 'day'], ['月', 'month']] %>
  </div>
  <div class="field">
    <%= f.label :validity_duration %><br>
    <%= f.text_field :validity_duration, type: 'number' %>
  </div>
  <div class="actions m-t-10">
    <%= link_to '取消', products_path, class: 'btn btn-default' %>
    <%= f.submit '保存', class: 'btn btn-primary' %>
  </div>
<% end %>

<% content_for :javascript do %>
<script>
  $(function(){
      $('form').validate();

      $('#product_validity_type').change(function(){
          if($(this).val() === 'forever'){
              $('#product_validity_duration').val('');
          }
      });

      $('#product_sale_start_at').datepicker({
          format: "yyyy-mm-dd",
          language: "zh-CN",
          weekStart: 1,
          autoclose: true,
          todayHighlight: true
      });

      $('#product_sale_end_at').datepicker({
          format: "yyyy-mm-dd",
          language: "zh-CN",
          weekStart: 1,
          autoclose: true,
          todayHighlight: true
      });
  });
</script>
<% end %>
